//
//  ChoiceViewController.m
//  项目PianKe
//
//  Created by 关雷 on 14-12-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ChoiceViewController.h"
#import "AFNetWoringYu.h"
#import "ReadChoicTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "ReadChoiceModel.h"
#import "ChoicUserModel.h"
#import "UIScrollView+MJRefresh.h"
#import "WebColor.h"
#import "ChoiceDetailViewController.h"
@interface ChoiceViewController ()<UITableViewDataSource, UITableViewDelegate, SDWebImageManagerDelegate>
@property (nonatomic, assign)NSInteger start;
@end

@implementation ChoiceViewController

- (void)dealloc
{
    [_table release];
    [_tableArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.frame = CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height - 45);
        self.tableArray = [NSMutableArray array];
        self.choicUserArray = [NSMutableArray array];
        
        self.view.backgroundColor = [WebColor gainsboro];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self startConnectWithUrl:@"http://api.pianke.me/read/recommend" body:nil];
    [self createTableView];
    [self headerRefreshing];
    [self footerLoading];
    // Do any additional setup after loading the view.

}



//网络请求,数据解析
- (void)startConnectWithUrl:(NSString *)url body:(NSString *)body
{
    [AFNetWoringYu netWorkWithURL:url body:body  resultBlock:^(id dic) {
        NSDictionary * adic = [dic objectForKey:@"data"];
        NSArray * array = [adic objectForKey:@"list"];
        for (NSDictionary * dics in array) {
            ReadChoiceModel * model = [[ReadChoiceModel alloc] init];
            [model setValuesForKeysWithDictionary:dics];
            if (model.userinfo != nil) {
                ChoicUserModel * choicuserM = [[ChoicUserModel alloc] init];
                [choicuserM setValuesForKeysWithDictionary:model.userinfo];
                [self.choicUserArray addObject:choicuserM];
                [choicuserM release];
            }
            [self.tableArray addObject:model];
            [model release];
        }
         [_table reloadData];
    }];
   

}

//创建tableView
- (void)createTableView
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 56)];
    _table.dataSource = self;
    _table.delegate = self;
    [_table registerClass:[ReadChoicTableViewCell class] forCellReuseIdentifier:@"cellChoice"];
    [self.view addSubview:_table];
    [_table release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}
//为cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.separatorColor = [UIColor clearColor];
    ReadChoicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellChoice"];
    cell.backgroundColor = [WebColor whiteSmoke];
    ReadChoiceModel * model = [_tableArray objectAtIndex:indexPath.row];
    ChoicUserModel * chModel = [_choicUserArray objectAtIndex:indexPath.row];
   
    [cell.imgV setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:nil];
    cell.titleLable.text = model.title;
    
    cell.nameLable.text = chModel.uname;
    cell.contentLable.text = model.content;
    
    return cell;
}
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 2;
}
//下拉刷新
- (void)headerRefreshing
{
    [_table addHeaderWithCallback:^{
        
        [AFNetWoringYu netWorkWithURL:@"http://api.pianke.me/read/recommend" body:nil  resultBlock:^(id dic) {
            [self.tableArray removeAllObjects];
            NSDictionary * adic = [dic objectForKey:@"data"];
            NSArray * array = [adic objectForKey:@"list"];
            for (NSDictionary * dics in array) {
                ReadChoiceModel * model = [[ReadChoiceModel alloc] init];
                [model setValuesForKeysWithDictionary:dics];
                if (model.userinfo != nil) {
                    ChoicUserModel * choicuserM = [[ChoicUserModel alloc] init];
                    [choicuserM setValuesForKeysWithDictionary:model.userinfo];
                    [self.choicUserArray addObject:choicuserM];
                    [choicuserM release];
                }
                [self.tableArray addObject:model];
                [model release];
            }
            [_table reloadData];
            [_table headerEndRefreshing];
        }];
    }];
}
//上拉加载
 - (void)footerLoading
{
    self.start = 10;
  
    [self.table addFooterWithCallback:^{
        if (_start < 1000) {
            NSString * str = [NSString stringWithFormat:@"%@%ld", @"client=1&limit=10&notoday=1&start=", self.start];
            [self startConnectWithUrl:@"http://api.pianke.me/read/recommend" body:str];
            [self.table footerEndRefreshing];
            _start += 10;
        }
        }];
}
//选中cell,进入详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ReadChoiceModel * model = [_tableArray objectAtIndex:indexPath.row];
    ChoiceDetailViewController * choiceD = [[ChoiceDetailViewController alloc] init];
    
    choiceD.hidesBottomBarWhenPushed = YES;
    choiceD.contentid = model.contentid;
    [self.navigationController pushViewController:choiceD animated:YES];
    [choiceD release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
